import { GetToken } from '@/utils/storage';

const initialState = {
  userInfo: {} as UserInfoType,
  Profile: {} as ProfileType,
  auth_token: GetToken(),
};

export type AuthTokenType = {
  refresh_token: string;
  token: string;
};

export type ProfileType = {
  id: string;
  photo: string;
  name: string;
  mobile: string;
  gender: number;
  birthday: string;
  intro?: string;
};

export type UserInfoType = {
  id: string;
  name: string;
  photo: string;
  art_count: number;
  follow_count: number;
  fans_count: number;
  like_count: number;
};

export default function userReducer(
  state = initialState,
  action: UserAction
): typeof initialState {
  switch (action.type) {
    case 'user/SaveAuthToken':
      return { ...state, auth_token: action.payload };
    case 'user/SaveUserInfo':
      return { ...state, userInfo: action.payload };
    case 'user/SaveProfile':
      return { ...state, Profile: action.payload };
    case 'user/UpdateProfile':
      return { ...state, Profile: { ...state.Profile, ...action.payload } };
    default:
      return state;
  }
}

export type UserAction =
  | {
      type: 'user/SaveAuthToken';
      payload: AuthTokenType;
    }
  | {
      type: 'user/SaveUserInfo';
      payload: UserInfoType;
    }
  | {
      type: 'user/SaveProfile';
      payload: ProfileType;
    }
  | {
      type: 'user/UpdateProfile';
      payload: Partial<ProfileType>;
    };
